<template>
	<view class="container-white">
		<view v-if="tag !=='3'" class="head flex-box">
			<text>{{name}}</text>
			<input class="m-input" type="text" focus v-model="value" />
		</view>
		<!-- <view v-else>
			<mix-tree :list="deptList" @checked="deptClick"></mix-tree>
		</view> -->
	</view>
</template>

<script>
	import {
		post_md5
	} from '../../common/request'
	import {
		setNavTitle
	} from './../../common/common'

	export default {
		data() {
			return {
				name: '',
				value: '',
				deptList: [],
				tag: null
			}
		},
		onLoad(options) {
			this.name = options.title || ''
			this.value = options.value || ''
			if (this.value === 'null') {
				this.value = ''
			}
			setNavTitle(options.title)
			this.tag = options.tag
			if (options.tag == 3) {
				this.getDept()
			}
		},
		onNavigationBarButtonTap() {
			switch (this.tag) {
				case '1':
					this.saveName()
					break
				case '2':
					this.savemobile()
					break
				case '3':
					this.saveDept()
					break
			}
		},
		methods: {
			getDept() {
				post_md5('/usercenter-app/user/getdeptlist').then(res => {
					res.data.forEach(item => {
						if (item.id == this.value) {
							item.isCheck = true
						} else {
							item.isCheck = false
						}
					})

					this.deptList = this.generateDeptTree(res.data)
				})
			},
			generateDeptTree(deptList) {
				let deptMap = new Map()
				deptList.forEach(dept => {
					deptMap.set(dept.id, dept)
				})

				let tree = []
				deptList.forEach(dept => {
					if (deptMap.has(dept.parentId)) {
						let parent = deptMap.get(dept.parentId)
						if (!parent.children) {
							parent.children = []
						}
						parent.children.push(dept)
					} else {
						tree.push(dept)
					}
				})
				return tree
			},
			deptClick(item) {
				this.value = item.id
			},
			saveName() {
				const params = {
					userId: uni.getStorageSync('userId'),
					realname: this.value
				}
				post_md5('/usercenter-app/user/editrealname', params).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '保存成功',
							icon: 'none'
						})
						uni.navigateBack()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			savemobile() {
				const params = {
					userId: uni.getStorageSync('userId'),
					mobile: this.value
				}
				post_md5('/usercenter-app/user/editmobile', params).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '保存成功',
							icon: 'none'
						})
						uni.navigateBack()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			saveDept() {
				const params = {
					userId: uni.getStorageSync('userId'),
					deptId: this.value
				}
				post_md5('/usercenter-app/user/editdeptid', params).then(res => {
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					})
					uni.navigateBack()
				})
			}
		}
	}
</script>

<style>
	.head {
		/* width: 100%; */
		color: #333333;
		font-size: 30upx;
		border-top: 1px solid rgba(125, 125, 125, 0.25);
		border-bottom: 1px solid rgba(125, 125, 125, 0.25);
		padding: 20upx 30upx;
	}

	.m-input {
		/* text-align: right; */
		margin-left: 60upx;
		flex: 1;
	}
</style>
